package com.qf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.common.http.Result;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.GoodsSku;
import com.qf.entity.po.LoginMerchant;
import com.qf.mapper.GoodsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {

    @Resource
    GoodsSkuService goodsSkuService;


    @Transactional(readOnly = true)
    public List<Goods> getByTypeId(Long goodsTypeId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (goodsTypeId > 0) {
            wrapper.lambda().eq(Goods::getGoodsTypeId, goodsTypeId);
        }
        wrapper.lambda().orderByAsc(Goods::getOrderNum);
        return this.list(wrapper);
    }

    @Transactional
    public void up(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId,goodsId).set(Goods::getState,Goods.State.UP.getCode());
        this.update(wrapper);
    }

    @Transactional
    public void down(Long goodsId) {
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId,goodsId).set(Goods::getState,Goods.State.DOWN.getCode());
        this.update(wrapper);
    }

    @Transactional(readOnly = true)
    public List<Goods> getByMerchantId(LoginMerchant loginMerchant) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId,loginMerchant.getId())
                        .orderByDesc(Goods::getCreateTime);
        return this.list(wrapper);
    }

    @Transactional
    public void saveOrUpdateByGoods(Goods goods,LoginMerchant loginMerchant) {
        if(goods.getId() != null){
            goodsSkuService.removeByGoodsId(goods.getId());
        }
        goods.setMerchantUserId(loginMerchant.getId());
        goods.setState(Goods.State.NON.getCode());
        this.saveOrUpdate(goods);

        String[] titles = goods.getSkuTitle().split("\\|");
        String[] costs = goods.getSkuCost().split("\\|");
        String[] prices = goods.getSkuPrice().split("\\|");
        String[] pmoneys = goods.getSkuPmoney().split("\\|");

        List<GoodsSku> needInsert = new ArrayList<>(titles.length);
        int orderNo = 1;
        for (int i = 0; i < titles.length; i++) {
            GoodsSku goodsSku = new GoodsSku();
            goodsSku.setGoodId(goods.getId());
            goodsSku.setTitle(titles[i]);
            goodsSku.setCost(costs[i]);
            goodsSku.setPrice(prices[i]);
            goodsSku.setPmoney(pmoneys[i]);
            goodsSku.setOrderNum(new Long(orderNo));
            needInsert.add(goodsSku);
            orderNo++;
        }

        goodsSkuService.saveBatch(needInsert, needInsert.size());
    }

    @Transactional
    public void removeGoods(Long id) {
        this.removeById(id);
        goodsSkuService.removeByGoodsId(id);
    }

    @Transactional(readOnly = true)
    public Goods getGoodsById(Long goodsId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getId,goodsId);
        return this.getOne(wrapper);
    }

    @Transactional(readOnly = true)
    public List<Goods> getByMerchantUserId(LoginMerchant loginMerchant) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId,loginMerchant.getId())
                        .eq(Goods::getState,Goods.State.UP.getCode())
                        .orderByDesc(Goods::getCreateTime);
        return this.list(wrapper);
    }
}